package com.sidianzhong.sdz.controller;


import com.sidianzhong.sdz.model.*;

import com.sidianzhong.sdz.service.*;

import com.sidianzhong.sdz.utils.ResultModel;
import com.sidianzhong.sdz.utils.SocketClient;
import com.sidianzhong.sdz.utils.SocketServer;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;

/**
 * Created by hxg on 2016/10/06.
 */
@Api(description = "巡机记录人员表")
@Controller
public class WebInspectionstaffrecordController {

    @Autowired
    WebInspectionstaffrecordService webInspectionstaffrecordService;

    @Autowired
    HttpServletResponse response;

    @ApiOperation(value = "创建认证人员")
    @RequestMapping(value = "/web_inspectionstaffrecords/new",
            method = RequestMethod.POST,
            produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object createWebInspectionstaffrecord(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "recordid", required = false) Integer recordid,
            @RequestParam(value = "staffname", required = false) String staffname
    ) {
        WebInspectionstaffrecord item = new WebInspectionstaffrecord();
        Date date = new Date();
        if (recordid != null) {
            item.setRecordid(recordid);
        }
        if (staffname != null) {
            item.setStaffname(staffname);
        }
        WebInspectionstaffrecord iteams = webInspectionstaffrecordService.create(item);

        return new ResponseEntity<>(ResultModel.ok(iteams), HttpStatus.OK);
    }


    private static String open = "{\"messageType\":\"APPCMD\",\"messageKey\":\"1565837652691\",\"equipmentType\":\"8\",\"equipmentKey\":\"1\",\"operatingType\":\"1\",\"operatingMessage\":[{\"SetType\":\"4\",\"SetParam\":\"1\"}]}";
    private static String close = "{\"messageType\":\"APPCMD\",\"messageKey\":\"1565837652691\",\"equipmentType\":\"8\",\"equipmentKey\":\"1\",\"operatingType\":\"1\",\"operatingMessage\":[{\"SetType\":\"4\",\"SetParam\":\"2\"}]}";

    @ApiOperation(value = "批量创建认证人员")
    @RequestMapping(value = "/getUsers",
            method = RequestMethod.GET,
            produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getUsers(
            HttpServletRequest request
    ) {
        if (!SocketServer.isOpen) {
            System.out.println("链接已启动");
            new Thread(() -> {
                SocketServer.isOpen = true;
                SocketClient.sendMessage(open);
                try {
                    Thread.sleep(10 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                SocketServer.isOpen = false;
                System.out.println("链接已关闭");
                SocketClient.sendMessage(close);
                SocketServer.list.clear();
            }).start();
        }
        return new ResponseEntity<>(ResultModel.ok(SocketServer.list), HttpStatus.OK);

    }
}
